﻿/************************************************
*
* Copyright(c) ZeeGuo.com
*
* CLR : 4.0.30319.296
*
* FileName : DownLoader.cs
*
* Author : Kurodo
*
* CreatTime : 2013/7/5 16:34:34
*           
*************************************************/

using System;
using System.Net;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace ComicSpider
{
    public class MultiDownLoader
    {
        public event AsyncCompletedEventHandler DownloadFilesCompleted;
        public int MaxThreadCount { get; set; }

        public string fileSize;
        private int currentThreadCount = 0;
        private int totalCount = 0;
        private int downloadedCount = 0;
        private Queue<Uri> downloadQueue = new Queue<Uri>();
        private string folder;

        public IComicStorage Storage
        {
            get;
            set;
        }

        public string Folder
        {
            get
            {
                return this.folder == null ? "D:" : this.folder;
            }
            set
            {
                this.folder = value;
            }
        }

        public bool CanStartThread
        {
            get
            {
                if (downloadQueue.Count == 0)
                    return false;
                if (currentThreadCount == MaxThreadCount)
                    return false;

                return true;
            }
        }

        public bool DownloadCompleted
        {
            get
            {
                return downloadedCount == totalCount;
            }
        }

        public MultiDownLoader(List<string> uriList)
        {
            //MaxThreadCount = 5;
            totalCount = uriList.Count;

            foreach (string strUri in uriList)
            {
                downloadQueue.Enqueue(new Uri(strUri));
            }
        }

        public void Start()
        {
            while (CanStartThread)
            {
                Uri uri = null;
                lock (downloadQueue)
                {
                    uri = downloadQueue.Dequeue();
                }

                if (uri == null)
                    continue;

                string file = Storage.StorageComic(uri.ToString(), this.Folder);

                WebClient client = new WebClient();
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(onCompleted);
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(onProgressChanged);
                client.DownloadFileAsync(uri, file);
            }
        }

        public void onProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            this.fileSize = e.TotalBytesToReceive.ToString();
        }

        public void onCompleted(object sender, AsyncCompletedEventArgs e)
        {
            downloadedCount++;
            if (DownloadCompleted)
            {
                AsyncCompletedEventArgs args = new AsyncCompletedEventArgs(null, false, null);
                DownloadFilesCompleted.Invoke(this, args);

                return;
            }

            Start();
        }
    }
}
